import {
  getConfigTable,
  editConfigRecord
} from '../../api/system'
import { forEach } from '@/libs/tools'
import {
  CONFIG_LIST,
  CONFIG_LIST_DATA,
  CONFIG_DATA_COUNT,
  CONFIG_HISTORY_DATA,
  CONFIG_CURRENT_ROW,
  CONFIG_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    configList: [],
    configListData: window.sessionStorage.getItem('config_list_data')
      ? JSON.parse(window.sessionStorage.getItem('config_list_data'))
      : {},
    configDataCount: null,
    configPageSize: 10,
    configHistoryData: [],
    row: {
      id: '',
      des: '',
      key: '',
      value: ''
    }
  },
  getters: {
    configList: state => state.configList
  },
  mutations: {
    [CONFIG_LIST] (state, data) {
      state.configList = data
    },
    [CONFIG_LIST_DATA] (state, data) {
      state.configListData = data
      window.sessionStorage.setItem('config_list_data', JSON.stringify(data))
    },
    [CONFIG_DATA_COUNT] (state, data) {
      state.configDataCount = data
    },
    [CONFIG_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [CONFIG_HISTORY_DATA] (state, data) {
      state.configHistoryData = data
    },
    [CONFIG_PAGE_SIZE] (state, data) {
      state.configPageSize = data
    }
  },
  actions: {
    async getConfigList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getConfigTable(data)
      let host = resp.data.host
      resp.data.data.map((item) => {
        if (item.key === 'freezeDay' || item.key === 'alter' || item.key === 'launchImg' || item.key === 'launchUrl' || item.key === 'cashMin') {
          newData = [...newData, {
            host: host,
            id: item.id,
            des: item.des,
            key: item.key,
            value: item.value
          }]
        }
      })
      commit(CONFIG_LIST, newData)
      commit(CONFIG_DATA_COUNT, newData.length)
      if (newData.length < rootState.parameterConfig.configPageSize) {
        commit(CONFIG_HISTORY_DATA, newData)
      } else {
        commit(CONFIG_HISTORY_DATA, newData.slice(0, rootState.parameterConfig.configPageSize))
      }
      return resp
    },
    async getConfigListData ({ commit, rootState }, playload) {
      window.sessionStorage.removeItem('config_list_data')
      let data = {}
      let newData = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getConfigTable(data)
      // console.log(resp)
      let host = resp.data.host
      newData.host = host
      forEach(resp.data.data, item => {
        if (item.key !== 'launchImg' && item.key !== 'launchUrl' && item.key !== 'cashMin' && item.key !== 'freezeDay' && item.key !== 'alter' && item.key !== 'host') {
          const { id, value } = item
          newData[item.key] = { id, value }
        }
      })
      commit(CONFIG_LIST_DATA, newData)
    },
    async editConfigData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editConfigRecord(data)
      return resp
    },
    async getConfigCurrentRow ({ commit, rootState }, row) {
      commit(CONFIG_CURRENT_ROW, row)
    },
    async changeConfigPage ({ commit, rootState }, index) {
      let data = rootState.parameterConfig.configList
      let _start = (index - 1) * rootState.parameterConfig.configPageSize
      let _end = index * rootState.parameterConfig.configPageSize
      commit(CONFIG_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeConfigPageSize ({ commit, rootState }, value) {
      commit(CONFIG_PAGE_SIZE, value)
    }
  }
}
